﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Library.Helper
{

    /// <summary>
    /// 日志文件助手类
    /// </summary>
    public class LogFileHelper
    {
        /// <summary>
        /// 默认日志路径
        /// </summary>
        public static string LogFileDefaultPath = FileHelper.GetCurrentAppPath() + @"/Logs/";

        /// <summary>
        /// 异常
        /// </summary>
        public const string LogType_Exception = "异常";

        /// <summary>
        /// 错误
        /// </summary>
        public const string LogType_Error = "错误";

        /// <summary>
        /// 警告
        /// </summary>
        public const string LogType_Warning = "警告";

        /// <summary>
        /// 信息
        /// </summary>
        public const string LogType_Information = "信息";


        /// <summary>
        /// 获取异常输出的字符串
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="strContent"></param>
        /// <returns></returns>
        public static string GetExceptionWriteString(Exception ex, string strContent = "")
        {
            StringBuilder sbLogMsg = new StringBuilder();
            if (!string.IsNullOrEmpty(strContent))
            {
                sbLogMsg.AppendLine(strContent);
            }
            sbLogMsg.AppendLine($"Execption-Message:{ex.Message}");
            sbLogMsg.AppendLine($"Exception-Method：{ex.TargetSite.DeclaringType.FullName.ToString() + " " + ex.TargetSite.ToString()}");
            sbLogMsg.AppendLine($"Execption-Source:{ex.Source}");
            sbLogMsg.AppendLine($"Execption-StackTrace:{ex.StackTrace}");
            sbLogMsg.AppendLine($"Execption-TargetSite:{ex.TargetSite}");
            sbLogMsg.AppendLine($"Execption-HelpLink:{ex.HelpLink}");
            if (ex.InnerException != null)
            {
                sbLogMsg.AppendLine($"Execption-InnerException-Message:{ex.InnerException.Message}");
                sbLogMsg.AppendLine($"Exception-InnerException-Method：{ex.InnerException.TargetSite.DeclaringType.FullName.ToString() + " " + ex.InnerException.TargetSite.ToString()}");
                sbLogMsg.AppendLine($"Execption-InnerException-Source:{ex.InnerException.Source}");
                sbLogMsg.AppendLine($"Execption-InnerException-StackTrace:{ex.InnerException.StackTrace}");
                sbLogMsg.AppendLine($"Execption-InnerException-TargetSite:{ex.InnerException.TargetSite}");
                sbLogMsg.AppendLine($"Execption-InnerException-HelpLink:{ex.InnerException.HelpLink}");
            }
            return sbLogMsg.ToString();
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="logTime">日志类别</param>
        /// <param name="strBusinessName">日志记录名称</param>
        /// <param name="strContent">日志内容</param>
        public static void WriteLog(string strBusinessName, string strContent)
        {
            try
            {
                WriteLog(LogFileDefaultPath, LogType_Error, strBusinessName, strContent);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="logTime">日志类别</param>
        /// <param name="strBusinessName">日志记录名称</param>
        /// <param name="strContent">日志内容</param>
        public static void WriteLog(string strLogType, string strBusinessName, string strContent)
        {
            try
            {
                WriteLog(LogFileDefaultPath, strLogType, strBusinessName, strContent);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="logTime">日志时间</param>
        /// <param name="logTime">日志类别</param>
        /// <param name="strBusinessName">日志记录名称</param>
        /// <param name="strContent">日志内容</param>
        public static void WriteLog(DateTime logTime, string strLogType, string strBusinessName, string strContent)
        {
            try
            {
                WriteLog(LogFileDefaultPath, logTime, strLogType, strBusinessName, strContent);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="strLogFilePath">日志文件路径</param>
        /// <param name="logTime">日志类别</param>
        /// <param name="strBusinessName">日志记录名称</param>
        /// <param name="strContent">日志内容</param>
        public static void WriteLog(string strLogFilePath, string strLogType, string strBusinessName, string strContent)
        {
            try
            {
                DateTime logTime = DateTime.Now;
                WriteLog(strLogFilePath, logTime, strLogType, strBusinessName, strContent);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="strLogFilePath">日志文件路径</param>
        /// <param name="logTime">日志类别</param>
        /// <param name="strBusinessName">日志记录名称</param>
        /// <param name="strContent">日志内容</param>
        public static void WriteLog(string strBusinessName, string strContent, Exception ex)
        {
            try
            {
                WriteLog(LogFileDefaultPath, strBusinessName, strContent, ex);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="strLogFilePath">日志文件路径</param>
        /// <param name="logTime">日志类别</param>
        /// <param name="strBusinessName">日志记录名称</param>
        /// <param name="strContent">日志内容</param>
        public static void WriteLog(string strLogFilePath, string strBusinessName, string strContent, Exception ex)
        {
            try
            {
                DateTime logTime = DateTime.Now;
                string strLogType = LogType_Exception;
                WriteLog(strLogFilePath, logTime, strLogType, strBusinessName, GetExceptionWriteString(ex, strContent));
            }
            catch
            {

            }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="strLogFilePath">日志文件路径</param>
        /// <param name="logTime">日志类别</param>
        /// <param name="strBusinessName">日志记录名称</param>
        /// <param name="strContent">日志内容</param>
        public static void WriteLog(string strLogFilePath, string strLogType, string strBusinessName, string strContent,Exception ex)
        {
            try
            {
                DateTime logTime = DateTime.Now;
                WriteLog(strLogFilePath, logTime, strLogType, strBusinessName, GetExceptionWriteString(ex,strContent));
            }
            catch
            {

            }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="strLogFilePath">日志文件路径</param>
        /// <param name="logTime">日志时间</param>
        /// <param name="logTime">日志类别</param>
        /// <param name="strBusinessName">日志记录名称</param>
        /// <param name="strContent">日志内容</param>
        public static void WriteLog(string strLogFilePath, DateTime logTime, string strLogType, string strBusinessName, string strContent)
        {
            try
            {
                string strTitle = "日志时间：" + logTime.ToString("yyyy-MM-dd HH:mm:ss");
                strTitle += "\r\n日志类别：" + strLogType;
                strTitle += "\r\n记录名称：" + strBusinessName + "\r\n日志内容为：\r\n";
                strContent += "\r\n================================================================\r\n";
                FileHelper.WriteLogCustom(strLogFilePath + logTime.ToString("yyyyMMdd"), strTitle + strContent);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 读取日志
        /// </summary>
        /// <param name="strFilePath"></param>
        /// <returns></returns>
        public static string ReadLog(string strFilePath)
        {
            string strRes = string.Empty;
            try
            {
                strRes = FileHelper.ReadLog(strFilePath);
            }
            catch (Exception ex)
            {
                return strRes;
            }
            return strRes;
        }

        /// <summary>
        /// 异常StringBuilder构造
        /// </summary>
        /// <param name="sbLog"></param>
        /// <param name="ex"></param>
        public static void ExceptionStringBuilder(StringBuilder sbLog, Exception ex)
        {
            try
            {
                if (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }
                sbLog.AppendLine($"Execption-Message:{ex.Message}");
                sbLog.AppendLine($"Exception Method：{ex.TargetSite.DeclaringType.FullName.ToString() + " " + ex.TargetSite.ToString()}");
                sbLog.AppendLine($"Execption-Source:{ex.Source}");
                sbLog.AppendLine($"Execption-StackTrace:{ex.StackTrace}");
                sbLog.AppendLine($"Execption-HelpLink:{ex.HelpLink}");
                sbLog.AppendLine($"Execption-TargetSite:{ex.TargetSite}");
            }
            catch
            {

            }
        }
    }
}
